//
//  AlertManager.m
//  wozhua
//
//  Created by 王洋 on 14/12/26.
//  Copyright (c) 2014年 王洋. All rights reserved.
//

#import "AlertManager.h"

@implementation CustomAlertAction

@end

@interface AlertManager ()

@property (copy, nonatomic) void (^buttonBlock)(NSInteger buttonIndex);

@end

@implementation AlertManager

+ (instancetype)sharedManager {
    static AlertManager *_sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedManager = [[AlertManager alloc] init];
    });
    
    return _sharedManager;
}

- (void)initActionSheetWithTitle:(NSString *)title cancelButton:(NSString *)cancelTitle destructiveButton:(NSString *)destructiveTitle otherButtons:(NSArray *)btns block:(void (^)(NSInteger buttonIndex))block inViewController:(UIViewController *)dvc {
    if (kIsAfterIOS8) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        NSInteger buttonIndex = 0;
        void (^actionBlock)(UIAlertAction *action) = ^(UIAlertAction *action) {
            if (block) {
                CustomAlertAction *realAction = (CustomAlertAction *)action;
                block(realAction.tag);
            }
        };
        if (destructiveTitle) {
            CustomAlertAction *destructiveAction = [CustomAlertAction actionWithTitle:destructiveTitle style:UIAlertActionStyleDestructive handler:actionBlock];
            destructiveAction.tag = buttonIndex;
            buttonIndex ++;
            [alertVC addAction:destructiveAction];
        }
        for (NSString *buttonTitle in btns) {
            CustomAlertAction *otherAction = [CustomAlertAction actionWithTitle:buttonTitle style:UIAlertActionStyleDefault handler:actionBlock];
            otherAction.tag = buttonIndex;
            buttonIndex ++;
            [alertVC addAction:otherAction];
        }
        if (cancelTitle) {
            CustomAlertAction *cancelAction = [CustomAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:actionBlock];
            cancelAction.tag = buttonIndex;
            [alertVC addAction:cancelAction];
        }
        
        [dvc presentViewController:alertVC animated:YES completion:nil];
    } else {
        _buttonBlock = block;
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTitle:destructiveTitle otherButtonTitles:nil];
        NSInteger buttonIndex = 0;
        if (destructiveTitle) {
            buttonIndex ++;
        }
        for (NSString *buttonTitle in btns) {
            [actionSheet addButtonWithTitle:buttonTitle];
            buttonIndex ++;
        }
        if (cancelTitle) {
            [actionSheet addButtonWithTitle:cancelTitle];
            actionSheet.cancelButtonIndex = buttonIndex;
        }
        
        [actionSheet showInView:dvc.view];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (self.buttonBlock) {
        self.buttonBlock(buttonIndex);
    }
}

- (void)initAlertViewWithTitle:(NSString *)title message:(NSString *)message cancelButton:(NSString *)cancelTitle otherButtons:(NSArray *)btns block:(void (^)(NSInteger buttonIndex))block inViewController:(UIViewController *)dvc {
    if (kIsAfterIOS8) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        NSInteger buttonIndex = 0;
        void (^actionBlock)(UIAlertAction *action) = ^(UIAlertAction *action) {
            if (block) {
                CustomAlertAction *realAction = (CustomAlertAction *)action;
                block(realAction.tag);
            }
        };
        if (cancelTitle) {
            CustomAlertAction *cancelAction = [CustomAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:actionBlock];
            cancelAction.tag = buttonIndex;
            buttonIndex ++;
            [alertVC addAction:cancelAction];
        }
        for (NSString *buttonTitle in btns) {
            CustomAlertAction *otherAction = [CustomAlertAction actionWithTitle:buttonTitle style:UIAlertActionStyleDefault handler:actionBlock];
            otherAction.tag = buttonIndex;
            buttonIndex ++;
            [alertVC addAction:otherAction];
        }
        
        [dvc presentViewController:alertVC animated:YES completion:nil];
    } else {
        _buttonBlock = block;
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles: nil];
        for (NSString *buttonTitle in btns) {
            [alertView addButtonWithTitle:buttonTitle];
        }
        
        [alertView show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (self.buttonBlock) {
        self.buttonBlock(buttonIndex);
    }
}

@end
